package com.example.yuanboot.common.config.web;

import com.example.yuanboot.common.anno.ApiRestController;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * webmvc
 *
 * @author qingyan 2021-06-22
 */
public class WebMvcConfig {
    //
//    @EnableAdminServer
//    @Configuration
//    @Order(Ordered.LOWEST_PRECEDENCE)
//    public static class AdminConfig extends WebSecurityConfigurerAdapter {
//        private String adminContextPath;
//
//        /**
//         * admin 不能和web服务一起，会有奇怪问题，静态文件配置失效
//         *
//         */
//        @Override
//        protected void configure(HttpSecurity http) throws Exception {
//            SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
//            successHandler.setTargetUrlParameter("redirectTo");
//            http.authorizeRequests()
//                    .antMatchers(adminContextPath + "/assets/**").permitAll()
//                    .antMatchers(adminContextPath + "/login").permitAll()
//                    .anyRequest().authenticated()
//                    .and()
//                    .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
//                    .logout().logoutUrl(adminContextPath + "/logout").and()
//                    .httpBasic().and()
//                    .csrf().disable();
//            /*http.authorizeRequests().antMatchers(adminContextPath + "/**")
//                    .permitAll();*/
//        }
//        @Autowired
//        public void setAdminContextPath(AdminServerProperties adminServerProperties) {
//            this.adminContextPath = adminServerProperties.getContextPath();
//        }
//    }
//
//
//
    @Configuration
    public static class WebConfig implements WebMvcConfigurer {

        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            String prefix = "/api";
            configurer
                    .addPathPrefix(prefix, c -> c.isAnnotationPresent(ApiRestController.class));
        }


        // prometheus+grafana+springboot2监控集成配置
//        @Bean
//        CommandLineRunner init(StorageService storageService) {
//            return (args) -> {
//                storageService.init();
//            };
//        }
        //prometheus+grafana+springboot2监控集成配置
/*        @Bean
        MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
            return registry -> {
                registry.config()
                        .commonTags("application", "yuan");
            };
        }*/


    }
}
